/* 
 * Copyright (c) 2012, Fromentin Xavier, Schnell Michaël, Dervin Cyrielle, Brabant Quentin
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *      * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *      * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *      * The names of its contributors may not be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL Fromentin Xavier, Schnell Michaël, Dervin Cyrielle OR Brabant Quentin 
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package kameleon.document;

import java.util.Iterator;

import kameleon.exception.InvalidIndexException;


/**
 * Represents a cell in an array.
 * 
 * <p>Contains all of the informations concerning a given cell from an array.
 * The content of the cell is stored as multiple paragraphs.
 * 
 * <p>Standard properties include: none.
 * 
 * @author		Fromentin Xavier, Schnell Michaël, Dervin Cyrielle, Brabant Quentin
 * @version		1.0
 * 
 * @see			Array
 */
public class Cell extends Paragraph implements Iterable<Paragraph> {

	/**
	 * Needed to serialize this class.
	 * 
	 * @see		java.io.Serializable
	 */
	private static final long serialVersionUID = 32165004821265771L ;

	/**
	 * Used to store the content of the cell.
	 */
	protected Paragraphs paragraphs ; 

	/**
	 * Builds an empty instance of {@code Cell}.
	 */
	public Cell() {
		super() ;
		this.paragraphs = new Paragraphs() ;
	}// Cell()

	/**
	 * Builds an instance of {@code Cell} with the given content.
	 *  
	 * @param 	tp
	 * 			content of the cell
	 */
	public Cell(TextParagraph tp) {
		this() ;
		this.append(tp) ;
	}// Cell(TextParagraph)

	/**
	 * Builds an instance of {@code Cell} with the given content.
	 *  
	 * @param 	p
	 * 			content of the cell
	 */
	public Cell(Paragraphs p) {
		this() ;
		this.paragraphs = p ;
	}// Cell(Paragraphs)

	/**
	 * Adds an instance of {@code Paragraph} at the specified index.
	 * 
	 * @param 	p
	 * 			instance of {@code Paragraph} added
	 * 
	 * @param 	index
	 * 			insert position
	 * 
	 * @throws	InvalidIndexException
	 * 			if the given index is out of range
	 * 			{@code index < 0 || index >= getCount()}
	 * 
	 * @see		Paragraphs#add(Paragraph, int)
	 */
	public void add(Paragraph p, int index) throws InvalidIndexException {
		this.paragraphs.add(p, index) ;
	}// add(Paragraph, int)

	/**
	 * Prepends an instance of {@code Paragraph}.
	 * 
	 * @param 	p
	 * 			instance of {@code Paragraph} prepended
	 * 
	 * @see		Paragraphs#push(Paragraph)
	 */
	public void push(Paragraph p) {
		this.paragraphs.push(p) ;
	}// push(Paragraph)

	/**
	 * Appends an instance of {@code Paragraph}.
	 * 
	 * @param 	p
	 * 			instance of {@code Paragraph} appended
	 * 
	 * @see		Paragraphs#append(Paragraph)
	 */
	public void append(Paragraph p) {
		this.paragraphs.append(p) ;
	}// append(Paragraph)

	/**
	 * Removes the instance of {@code Paragraph} found at the given index.
	 * 
	 * @param 	index
	 * 			index of the removed {@code Paragraph}
	 * 
	 * @throws	InvalidIndexException
	 * 			if the given index is out of range
	 * 			{@code index < 0 || index >= getCount()}
	 * 
	 * @see		Paragraphs#remove(int)
	 */
	public void remove(int index) throws InvalidIndexException {
		this.paragraphs.remove(index) ;
	}// remove(int)

	/**
	 * Removes all the instances matching the given {@code Paragraph}.
	 * 
	 * @param 	p
	 * 			removed {@code Paragraph}
	 * 
	 * @see		Paragraphs#remove(Paragraph)
	 */
	public void remove(Paragraph p) {
		this.paragraphs.remove(p) ;
	}// remove(Paragraph)

	/**
	 * Returns the number of instance of {@code Paragraph} contained in this instance.
	 * 
	 * @return	Number of instance of {@code Paragraph} contained in this instance
	 */
	public int getCount() {
		return this.paragraphs.getCount() ;
	}// getCount()

	/**
	 * Returns an iterator for this instance.
	 * 
	 * <p>The iterator iterates over the paragraphs sequentially.
	 * 
	 * @return	an instance of {@code Iterator<Paragraph>} for this instance
	 */
	@Override
	public Iterator<Paragraph> iterator() {
		return this.paragraphs.iterator() ;
	}// iterator()
	
	/**
	 * Returns all the paragraphs of this instance.
	 * 
	 * @return	instance of {@code Paragraphs} containing all the paragraphs
	 */
	public Paragraphs getParagraphs() {
		return this.paragraphs ;
	}// getParagraphs()

}// class Cell